﻿<?php 
require '../../frame/base.php';

$tag = !empty($_REQUEST['tag'])?$_REQUEST['tag']:null;
$shopid = !empty($_REQUEST['shopid'])?$_REQUEST['shopid']:null;
$goodsid = !empty($_REQUEST['goodsid'])?$_REQUEST['goodsid']:null;


// 时间查询
if($_REQUEST['date']!="" && $_REQUEST['date']!=""){
	$date=$_REQUEST['date'];
	$late=$_REQUEST['latedate'];
	$times=array($date,$late);
}else{
	$date=date('Y-m-d');
	$late=date('Y-m-d',strtotime($date)+3600*24);
	$times=array($date,$late);
}

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=20;
}
// 分页 end

if($tag=='userinfo'){
	if($shopid){
		$recordScan['shop']=$Db->table('shop')->field('shopid,name,images')->where('shopid',$shopid)->find();
		$shops=$Db->table('scan_record')->where('sid',$shopid)->whereTime('created_at','between',$times)->limit($de,$size)->select();


		foreach($shops as $k=>$vlue){
			$recordScan['goods'][$k]['num']=$Db->table('scan_record')->distinct(TRUE)->field('uid')->where('goodsid',$vlue['goodsid'])->whereTime('created_at','between',$times)->limit($de,$size)->count();
			$good=$Db->table('scan_record')->field('title,images,yuanjia,price')->where('goodsid',$vlue['goodsid'])->whereTime('created_at','between',$times)->limit($de,$size)->find();
			$recordScan['goods'][$k]['gid']=$vlue['goodsid'];
			$recordScan['goods'][$k]['title']=$good['title'];
			$recordScan['goods'][$k]['images']=$good['images'];
			$recordScan['goods'][$k]['yuanjia']=$good['yuanjia'];
			$recordScan['goods'][$k]['price']=$good['price'];
		}
		$ary=$recordScan;
	}else{
		$ary=[];
	}
}
elseif($tag=='goodsinfo'){

	if($goodsid){
		$joins=array(['user us','sr.uid=us.userid','LEFT']);
		$recordScan1=$Db->table('scan_record')->where('goodsid',$goodsid)->whereTime('created_at','between',$times)->count();
	// 商品浏览
		$recordScan2=$Db->table('scan_record')->alias('sr')->join($joins)->distinct(true)->field('us.userid,us.niname,us.images,sr.position,sr.created_at')->where('goodsid',$goodsid)->whereTime('created_at','between',$times)->limit($de,$size)->select();
		$ary=['scanNum'=>$recordScan1,'scanUser'=>$recordScan2];
	}else{
		$ary=[];
	}

}elseif($tag=='all'){

}
echo json_encode(['code'=>200,'msg'=>'成功','data'=>$ary]);
